<script setup>
import { ref, onMounted } from 'vue'

const leftDrawerOpen = ref(false)
const currentUser = ref(null)

function toggleLeftDrawer() {
  leftDrawerOpen.value = !leftDrawerOpen.value
}

function logout() {
  localStorage.removeItem('user')
  currentUser.value = null
  // 如果当前页面需要登录，可以在此处重定向到首页
}

onMounted(() => {
  const userJson = localStorage.getItem('user')
  if (userJson) {
    try {
      currentUser.value = JSON.parse(userJson)
    } catch (e) {
      console.error('Failed to parse user data', e)
      localStorage.removeItem('user')
    }
  }
})

const links = [
  { title: '首页', icon: 'home', route: '/' },
  { title: '个人简介', icon: 'person', route: '/profile' },
  { title: '项目经历', icon: 'work', route: '/projects' },
  { title: '短文', icon: 'article', route: '/articles' },
  { title: '留言板', icon: 'message', route: '/message-board' },
  { title: 'AI 助手', icon: 'smart_toy', route: '/ai-assistant' }
]
</script>

<template>
  <q-layout view="hHh lpR fFf">
    <!-- 顶部导航栏 -->
    <q-header elevated class="bg-primary text-white">
      <q-toolbar>
        <q-btn
          dense
          flat
          round
          icon="menu"
          @click="toggleLeftDrawer"
          class="q-mr-sm"
        />

        <q-toolbar-title>
          <span class="site-title">个人主页</span>
        </q-toolbar-title>

        <div class="q-gutter-sm">
          <q-btn v-for="link in links" :key="link.title" :to="link.route" flat dense>
            {{ link.title }}
          </q-btn>
        </div>

        <q-space />

        <template v-if="currentUser">
          <q-btn-dropdown flat dense icon="person" class="user-menu">
            <q-list>
              <q-item>
                <q-item-section avatar>
                  <q-avatar>
                    <img :src="currentUser.avatar">
                  </q-avatar>
                </q-item-section>
                <q-item-section>
                  <q-item-label>{{ currentUser.name }}</q-item-label>
                  <q-item-label caption>{{ currentUser.username }}</q-item-label>
                </q-item-section>
              </q-item>

              <q-separator />

              <q-item clickable v-close-popup to="/profile">
                <q-item-section avatar>
                  <q-icon name="settings" />
                </q-item-section>
                <q-item-section>
                  个人设置
                </q-item-section>
              </q-item>

              <q-item clickable v-close-popup @click="logout">
                <q-item-section avatar>
                  <q-icon name="logout" />
                </q-item-section>
                <q-item-section>
                  退出登录
                </q-item-section>
              </q-item>
            </q-list>
          </q-btn-dropdown>
        </template>
        <template v-else>
          <q-btn flat dense to="/login" label="登录" class="q-mr-sm" />
          <q-btn unelevated dense to="/register" label="注册" color="white" text-color="primary" />
        </template>
      </q-toolbar>
    </q-header>

    <!-- 侧边导航抽屉 (移动设备) -->
    <q-drawer v-model="leftDrawerOpen" bordered class="bg-white">
      <q-scroll-area style="height: 100%;">
        <q-list>
          <template v-if="currentUser">
            <q-item class="q-pa-md">
              <q-item-section avatar>
                <q-avatar size="56px">
                  <img :src="currentUser.avatar">
                </q-avatar>
              </q-item-section>
              <q-item-section>
                <q-item-label class="text-weight-bold">{{ currentUser.name }}</q-item-label>
                <q-item-label caption>{{ currentUser.username }}</q-item-label>
              </q-item-section>
            </q-item>
          </template>
          <template v-else>
            <div class="q-pa-md">
              <q-btn to="/login" color="primary" label="登录" class="full-width q-mb-sm" />
              <q-btn to="/register" outline color="primary" label="注册" class="full-width" />
            </div>
          </template>

          <q-separator />
          
          <q-item-label header>导航菜单</q-item-label>
          
          <q-item 
            v-for="link in links"
            :key="link.route"
            :to="link.route"
            clickable
            v-ripple
          >
            <q-item-section avatar>
              <q-icon :name="link.icon" />
            </q-item-section>
            <q-item-section>
              {{ link.title }}
            </q-item-section>
          </q-item>

          <q-separator v-if="currentUser" />

          <q-item v-if="currentUser" clickable @click="logout" v-ripple>
            <q-item-section avatar>
              <q-icon name="logout" />
            </q-item-section>
            <q-item-section>
              退出登录
            </q-item-section>
          </q-item>
        </q-list>
      </q-scroll-area>
    </q-drawer>

    <!-- 页面主体内容 -->
    <q-page-container>
      <q-page>
        <router-view v-slot="{ Component }">
          <transition
            name="fade"
            mode="out-in"
            appear
          >
            <component :is="Component" />
          </transition>
        </router-view>
      </q-page>
    </q-page-container>

    <!-- 页脚 -->
    <q-footer bordered class="bg-white text-grey-8">
      <q-toolbar>
        <q-toolbar-title class="text-center">
          <div class="text-caption">© 2024 My Personal Website. All rights reserved.</div>
        </q-toolbar-title>
      </q-toolbar>
    </q-footer>
  </q-layout>
</template>

<style>
body {
  background: #f5f5f5;
}

.site-title {
  font-weight: 700;
  letter-spacing: 0.5px;
}

.q-page {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

/* Page transition effects */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease, transform 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
  transform: translateY(10px);
}

@media (max-width: 600px) {
  .q-page {
    padding: 10px;
  }
}

.user-menu .q-avatar {
  width: 28px;
  height: 28px;
}
</style>
